<template>
  <el-container class="home-container">
    <!-- 头部区域 -->
    <el-header class="el-header">
      <div>
        <img src="../assets/heima.png" />
        <span>电商后台管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <!-- 内容区域 -->
    <el-container>
      <!-- 左边区域 -->
      <!-- 折叠效果 -->
      <el-aside :width="isCollapse ? '60px' : '200px'" class="el-aside">
        <div class="toggle-button" @click="toggleButton">|||</div>
        <el-menu
          background-color="#333714"
          text-color="#fff"
          active-text-color="#409eff"
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="active"
        >
          <el-submenu :index="item.id+''" v-for="item in MenuList" :key="item.id">
            <!-- element-ui中自带的跳转方式 -->
            <!-- 上面的index因为只接受字符串,所以后面 拼接了一个空的字符串 下面也是一样 -->
            <!-- 一级导航 -->
            <template slot="title">
              <i class="el-icon-location"></i>
              <span>{{item.authName}}</span>
            </template>
            <!-- 二级导航的分类 -->
            <el-menu-item
              :index=" '/'+ subItem.path"
              v-for="subItem in item.children"
              :key="subItem.id"
              @click="activeNav('/'+ subItem.path)"
            >
              <template slot="title">
                <i class="el-icon-menu"></i>
                <span>{{subItem.authName}}</span>
              </template>
            </el-menu-item>
          </el-submenu>
        </el-menu>
      </el-aside>
      <!-- 右边区域 -->
      <el-main class="el-main">
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>
<script>
export default {
  data() {
    return {
      MenuList: [],
      isCollapse: false,
      active:""
    };
  },
  created() {
    this.getMenuList();
    this.active=window.sessionStorage.getItem("activePath")
  },
  methods: {
    logout() {
      //清空token
      window.sessionStorage.clear();
      //跳转到登录页面
      this.$router.push("/login");
    },
    async getMenuList() {
      const { data: res } = await this.axios.get("menus");
      if (res.meta.status !== 200) return this.$message.error(res.meta.msg);
      console.log(res);
      this.MenuList = res.data;
    },
    toggleButton() {
      this.isCollapse = !this.isCollapse;
    },
    activeNav(allActive){
      window.sessionStorage.setItem("activePath",allActive)
      this.active=allActive
    }
  }
};
</script>
<style lang="less" scoped>
.home-container {
  height: 100%;
}
.el-header {
  background-color: #373d41;
  padding-right: 20px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 20px;
  color: #eaedf1;
  div {
    display: flex;
    align-items: center;
    span {
      margin-left: 10px;
    }
  }
}
.el-aside {
  background-color: #333714;
  .toggle-button {
    height: 30px;
    width: 100%;
    background-color: burlywood;
    text-align: center;
    line-height: 30px;
    letter-spacing: 0.2em;
    cursor: pointer;
  }
}
.el-main {
  background-color: #eaedf1;
}
</style>
